package com.trekmapper.client.country;

import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author Daniel Jodar
 *
 */
public class CreateCountry implements EntryPoint {

	private Label nameLbl = new Label();
	private TextBox nameTxt = new TextBox();
	
	private Button createBtn = new Button();
	private Button findBtn = new Button();
	
	private final CountryServiceAsync countryService = GWT.create(CountryService.class);
	
	/* (non-Javadoc)
	 * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
	 */
	@Override
	public void onModuleLoad() {
		nameLbl.setText("Name");
		createBtn.setText("Create");
		findBtn.setText("Find");
		
		nameTxt.setFocus(true);
		nameTxt.selectAll();
		
		// Create the popup dialog box
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Remote Procedure Call");
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button("Close");
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		final Label textToServerLabel = new Label();
		final HTML serverResponseLabel = new HTML();
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);
		
		// disposition of panels
		VerticalPanel mainPanel = new VerticalPanel(); 
		HorizontalPanel namePanel = new HorizontalPanel();
		namePanel.setSpacing(5);
		
		namePanel.add(nameLbl);
		namePanel.add(nameTxt);
		
		mainPanel.add(namePanel);
		mainPanel.add(createBtn);
		mainPanel.add(findBtn);
		mainPanel.setBorderWidth(1);
		
		RootPanel.get("createCountryPanel").add(mainPanel);
		
		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				createBtn.setEnabled(true);
				createBtn.setFocus(true);
			}
		});
		
		findBtn.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				countryService.find(new AsyncCallback<List<Country>>(){

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(List<Country> result) {
						dialogBox.setText("Countries found");
						serverResponseLabel.removeStyleName("serverResponseLabelError");
						String resultLabel = "";
						for(Country country : result){
							resultLabel += country;
						}
						serverResponseLabel.setHTML(resultLabel);
						dialogBox.center();
						closeButton.setFocus(true);						
					}
					
				});			
			}
			
		});
		
		// Create a handler for the createButton
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the create
			 */
			public void onClick(ClickEvent event) {
				sendNameToServer();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendNameToServer();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a response.
			 */
			private void sendNameToServer() {
				createBtn.setEnabled(false);
				String textToServer = nameTxt.getText();
				textToServerLabel.setText(textToServer);
				serverResponseLabel.setText("");
				countryService.insert(textToServer,
						new AsyncCallback<String>() {
							public void onFailure(Throwable caught) {
								 // Show the RPC error message to the user
								dialogBox.setText("Remote Procedure Call - Failure");
								serverResponseLabel.addStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML("Error Dani");
								dialogBox.center();
								closeButton.setFocus(true);
							}

							public void onSuccess(String result) {
								dialogBox.setText("Remote Procedure Call Aoife");
								serverResponseLabel
										.removeStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(result);
								dialogBox.center();
								closeButton.setFocus(true);
							}
						});
			}
		}
		
		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		createBtn.addClickHandler(handler);
		nameTxt.addKeyUpHandler(handler);
		
	}

}
